package cn.edu.nuaa.aadl2.generatorC.utils;

import org.osate.aadl2.impl.IntegerLiteralImpl;
import org.osate.ba.aadlba.Factor;
import org.osate.ba.aadlba.SimpleExpression;
import org.osate.ba.aadlba.Value;

public class BitsUtils {
	private static final String TYPE_OF_16_BITS="short";
	private static final String TYPE_OF_32_BITS="int";
	private static final String TYPE_OF_64_BITS="long";
	public static BitsExpression handleBitsExpression(SimpleExpression baExpression) {
		Value value=baExpression.getTerms().get(0).getFactors().get(0).getFirstValue();
//		return new BitsExpression(TYPE_OF_16_BITS, "0xa1b2");
		System.out.println(value);
		if (value instanceof IntegerLiteralImpl) {
			int base=(int) ((IntegerLiteralImpl)value).getBase();
			long value0 = ((IntegerLiteralImpl)value).getValue();
			String expression=getRawBits(base,value0);
			if (base==16) {
				return new BitsExpression(TYPE_OF_16_BITS, expression);
			} else if(base==32) {
				return new BitsExpression(TYPE_OF_32_BITS, expression);
			}else if(base==64){
				return new BitsExpression(TYPE_OF_64_BITS, expression);
			}
		}
		return null;
	}
	private static final String ZERO_STRING="0";
	private static String getRawBits(int base,long value0) {
		String str=Long.toHexString(value0);
		System.out.println(str);
		while (str.length()<4) {
			str=ZERO_STRING+str;
		}
		return "0x"+str;
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}
	
	public static class BitsExpression{
		public String type;
		public String expression;
		
		private BitsExpression(String type,String expression) {
			this.type=type;
			this.expression=expression;
			// TODO Auto-generated constructor stub
		}
	}

}
